package com.education.teach.mvp.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dn.res.wideget.CustomLoadMoreView;
import com.dn.res.wideget.DnToolbar;
import com.education.teach.mvp.model.bean.CoursewareBean;
import com.education.teach.mvp.model.bean.LoginBean;
import com.education.teach.mvp.model.bean.MyCoursewareBean;
import com.education.teach.mvp.ui.adapter.CoursewareAdapter;
import com.education.teach.mvp.ui.adapter.MyCoursewareAdapter;
import com.education.teach.utils.DnCommonUtils;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.education.teach.di.component.DaggerMyCoursewareActivityComponent;
import com.education.teach.di.module.MyCoursewareActivityModule;
import com.education.teach.mvp.contract.MyCoursewareActivityContract;
import com.education.teach.mvp.presenter.MyCoursewareActivityPresenter;

import com.education.teach.R;
import com.tbruyelle.rxpermissions2.RxPermissions;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import dn.easy.common.commonutils.TimeUtil;
import dn.easy.common.commonwidget.AVLoadingDialog;

import static com.jess.arms.utils.Preconditions.checkNotNull;

// 我的课件
public class MyCoursewareActivityActivity extends BaseActivity<MyCoursewareActivityPresenter> implements MyCoursewareActivityContract.View, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.toolbar)
    DnToolbar mToolbar;
    @BindView(R.id.lv_content)
    RecyclerView mRecyclerView;
    @BindView(R.id.swf_pager_list)
    SwipeRefreshLayout mSwipeRereshLayout;
    int maxRow = 10;
    int page = 1;
    private List<MyCoursewareBean.ResultBean.QueryListBean> mList = new ArrayList<>();
    private MyCoursewareAdapter mAdapter;
    private LoginBean mLoginBean;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMyCoursewareActivityComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .myCoursewareActivityModule(new MyCoursewareActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_my_courseware; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mToolbar.setMainTitle("我的课件");
        mToolbar.setToolbarLeftBackImageRes(R.mipmap.top_back);
        mToolbar.setOnLeftTitleClickListener(view -> finish());
        mLoginBean = DnCommonUtils.getUserModel(this);
        mSwipeRereshLayout.setOnRefreshListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MyCoursewareAdapter(R.layout.item_courseware, mList, this);
        CustomLoadMoreView mLoadMoreView = new CustomLoadMoreView();
        mAdapter.setLoadMoreView(mLoadMoreView);
        mAdapter.setOnLoadMoreListener(this, mRecyclerView);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        mAdapter.isFirstOnly(true);
        mRecyclerView.setAdapter(mAdapter);
        mPresenter.searchDocList(mLoginBean.getResult().getXsXlh(), page, maxRow, true);


        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(MyCoursewareActivityActivity.this, CoursewareDeatilActivity.class);
            intent.putExtra("title", mAdapter.getData().get(position).getDocument().getDocumentName());
            intent.putExtra("desp", mAdapter.getData().get(position).getDocument().getDesp());
            intent.putExtra("fileType", mAdapter.getData().get(position).getDocument().getFileType());
            intent.putExtra("createName", mAdapter.getData().get(position).getDocument().getJiaoShiName());
            intent.putExtra("realUrl", mAdapter.getData().get(position).getDocument().getRealUrl());
            intent.putExtra("url", mAdapter.getData().get(position).getDocument().getUrl());
            intent.putExtra("date", TimeUtil.formatData(TimeUtil.dateFormatYMDHMofChinese, mAdapter.getData().get(position).getDocument().getCreateDate()));
            launchActivity(intent);
        });

        mAdapter.setOnItemChildClickListener((BaseQuickAdapter adapter, View view, int position) -> {
            RxPermissions rxPermissions = new RxPermissions(this);
            rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .subscribe(granted -> {
                        mPresenter.loadFile(mAdapter.getData().get(position).getDocument().getUrl(), mAdapter.getData().get(position).getDocument().getRealUrl());
                    });
        });
    }

    @Override
    public void showLoading() {
        AVLoadingDialog.showDialogForLoading(this);
    }

    @Override
    public void hideLoading() {
        if (mSwipeRereshLayout != null) mSwipeRereshLayout.setRefreshing(false);
        AVLoadingDialog.cancelDialogForLoading();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onRefresh() {
        page = 1;
        mPresenter.searchDocList(mLoginBean.getResult().getXsXlh(), page, maxRow, true);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        mPresenter.searchDocList(mLoginBean.getResult().getXsXlh(), page, maxRow, false);
    }

    @Override
    public void returnRefreshDocList(List<MyCoursewareBean.ResultBean.QueryListBean> queryList) {
        if (mSwipeRereshLayout != null) mSwipeRereshLayout.setRefreshing(false);
        if (queryList != null)
            mAdapter.setNewData(queryList);
        if (queryList == null || queryList.size() < maxRow)
            mAdapter.loadMoreEnd(true);
    }

    @Override
    public void returnLoadMoreDocList(List<MyCoursewareBean.ResultBean.QueryListBean> queryList) {
        mAdapter.loadMoreComplete();
        mAdapter.addData(queryList == null ? new ArrayList<>() : queryList);
    }
}
